//
//  AFAccountViewController.m
//  AFChat
//
//  Created by Void on 2018/6/20.
//  Copyright © 2018年 afei. All rights reserved.
//

#import "AFAccountViewController.h"
#import "AFLoginViewController.h"
#import "AFUserHelper.h"

#define     HEIGHT_BUTTON       50
#define     EDGE_BUTTON         35

typedef NS_ENUM(NSInteger,AFAccountButtonType) {
    AFAccountButtonTypeRegister,
    AFAccountButtonTypeLogin,
    AFAccountButtonTypeTest,
};

@interface AFAccountViewController ()
@end

@implementation AFAccountViewController

- (void)loadView{
    [super loadView];
    
    CGSize viewSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    NSString *viewOrientation = @"Portrait";    //横屏请设置成 @"Landscape"
    NSArray *launchImages = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    NSString *launchImageName = nil;
    for (NSDictionary *dict in launchImages) {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    
    UIImageView *imageView = UIImageView.zz_create(1).image(TLImage(launchImageName)).view;
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    
    UIButton *(^creatButton)(NSString *title, UIColor *bgColor, NSInteger tag) = ^UIButton *(NSString *title, UIColor *bgColor, NSInteger tag) {
      UIButton *button = UIButton.zz_create(tag)
        .backgroundColor(bgColor)
        .title(title).titleFont([UIFont systemFontOfSize:19])
         .cornerRadius(5.0f)
        .view;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    };
    
    UIButton *registerButton = creatButton(@"注 册",[UIColor redColor], AFAccountButtonTypeRegister);
    [self.view addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(EDGE_BUTTON);
        make.bottom.mas_equalTo(-EDGE_BUTTON * 2);
        make.width.mas_equalTo((SCREEN_WIDTH - EDGE_BUTTON * 3) / 2);
        make.height.mas_equalTo(HEIGHT_BUTTON);
    }];
    
    UIButton *loginButton = creatButton(@"登 录",[UIColor colorGreenDefault], AFAccountButtonTypeLogin);
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-EDGE_BUTTON);
        make.size.and.bottom.mas_equalTo(registerButton);
    }];
    
    UIButton *textButton = creatButton(@"使用测试账号登录", [UIColor clearColor], AFAccountButtonTypeTest);
    [self.view addSubview:textButton];
    [textButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(EDGE_BUTTON);
        make.right.mas_equalTo(-EDGE_BUTTON);
        make.height.mas_equalTo(HEIGHT_BUTTON);
    }];
    
}

- (void)buttonClick:(UIButton *)sender {
    
    if (sender.tag == AFAccountButtonTypeRegister) {
    }else if (sender.tag == AFAccountButtonTypeLogin) {
        AFLoginViewController *loginVC = [[AFLoginViewController alloc] init];
        TLWeakSelf(self);
        TLWeakSelf(loginVC);
        [weakloginVC setLoginSuccess:^{
            [weakloginVC dismissViewControllerAnimated:YES completion:nil];
            if (weakself.loginSuccess) {
                weakself.loginSuccess();
            }
        }];
        [self presentViewController:loginVC animated:YES completion:nil];
    }else if (sender.tag ==AFAccountButtonTypeTest) {
        [[AFUserHelper sharedHelper] loginTestAccount];
        if (self.loginSuccess) {
            self.loginSuccess();
        }
    }
}

@end
